package com.tusuo.stds.ui.main;

import android.content.Intent;
import android.os.Handler;
import android.os.SystemClock;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.TextView;

import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.tusuo.stds.MyApp;
import com.tusuo.stds.R;
import com.tusuo.stds.biz.CurrentUser;
import com.tusuo.stds.biz.PageStatus;
import com.tusuo.stds.di.DaggerScope;
import com.tusuo.stds.di.Injector;
import com.tusuo.stds.rest.ApiHelper;
import com.tusuo.stds.rest.CatalogList;
import com.tusuo.stds.ui.BaseToolbarFragment;
import com.tusuo.stds.util.webview.WebViewManager;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;
import org.androidannotations.annotations.res.StringRes;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import autodagger.AutoComponent;
import autodagger.AutoInjector;
import rx.Subscriber;

import static com.tusuo.stds.Constants.WEB_PAGE_FIND;

/**
 * Created by developer on 17-8-28.
 */

@AutoComponent(
        dependencies = MyApp.class
)
@AutoInjector
@DaggerScope(FindFragment.class)
@EFragment(R.layout.frag_find)
public class FindFragment extends BaseToolbarFragment {

    @ViewById(R.id.frag_find_web)
    BridgeWebView webView;
    @ViewById(R.id.frag_find_layout_refresh)
    SmartRefreshLayout layoutRefresh;

    @Inject
    CurrentUser mCurrentUser;
    @Inject
    ApiHelper mApiHelper;

    @StringRes(R.string.toolbar_title_find)
    String toolbarTitle;

    private WebViewManager mWebViewManager;
    private boolean mFirst = false;

    @AfterInject
    void initDI() {
        Injector.initFindFragmentComponent().inject(this);
    }

    @AfterViews
    void initData() {
        PageStatus pageStatus = new PageStatus(true, false, toolbarTitle);
        initData(pageStatus,null);

        mWebViewManager = new WebViewManager(getActivity(),webView);
        mWebViewManager.init();
        mFirst = true;

        layoutRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(1000);
                mWebViewManager.reload();
            }
        });
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);

        if (!hidden && mFirst) {
            mWebViewManager.loadUrl(WEB_PAGE_FIND);
            mFirst = false;
        }
    }

    @Override
    protected void findViews() {
        mToolbar = (Toolbar) getActivity().findViewById(R.id.find_toolbar);
        mToolbarTitle = (TextView) getActivity().findViewById(R.id.find_tv_toolbar_title);
    }

}
